package com.atguigu.lease.web.app.service.impl;

import com.atguigu.lease.common.context.LoginUserContext;
import com.atguigu.lease.model.entity.*;
import com.atguigu.lease.model.enums.ItemType;
import com.atguigu.lease.model.enums.LeaseStatus;
import com.atguigu.lease.model.enums.ReleaseStatus;
import com.atguigu.lease.web.app.mapper.*;
import com.atguigu.lease.web.app.service.*;
import com.atguigu.lease.web.app.vo.apartment.ApartmentItemVo;
import com.atguigu.lease.web.app.vo.attr.AttrValueVo;
import com.atguigu.lease.web.app.vo.fee.FeeValueVo;
import com.atguigu.lease.web.app.vo.graph.GraphVo;
import com.atguigu.lease.web.app.vo.room.RoomDetailVo;
import com.atguigu.lease.web.app.vo.room.RoomItemVo;
import com.atguigu.lease.web.app.vo.room.RoomQueryVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author liubo
 * @description 针对表【room_info(房间信息表)】的数据库操作Service实现
 * @createDate 2023-07-26 11:12:39
 */
@Service
@Slf4j
public class RoomInfoServiceImpl extends ServiceImpl<RoomInfoMapper, RoomInfo> implements RoomInfoService {

    @Autowired
    private RoomInfoMapper roomInfoMapper;

    @Autowired
    private ApartmentInfoService apartmentInfoService;

    @Autowired
    private GraphInfoService graphInfoService;

    @Autowired
    private AttrValueService attrValueService;

    @Autowired
    private FacilityInfoService facilityInfoService;

    @Autowired
    private LabelInfoService labelInfoService;

    @Autowired
    private FeeValueService feeValueService;

    @Autowired
    private PaymentTypeService paymentTypeService;

    @Autowired
    private LeaseTermService leaseTermService;

    @Autowired
    private LeaseAgreementService leaseAgreementService;

    @Autowired
    private BrowsingHistoryService browsingHistoryService;

    @Override
    public IPage<RoomItemVo> getRoomItemVoPage(Page<RoomItemVo> page, RoomQueryVo queryVo) {
        return roomInfoMapper.getRoomListById(page, queryVo);
    }

    @Override
    public RoomDetailVo getRoomDetailById(Long id) {
        //查找RoomInfo
        RoomInfo roomInfo = this.getById(id);
        if (roomInfo == null){
            return null;
        }
        RoomDetailVo roomDetailVo = new RoomDetailVo();

        //查找公寓vo
        ApartmentItemVo apartmentItemVo = apartmentInfoService.getApartmentItemVoById(roomInfo.getApartmentId());

        //查找图片列表
        List<GraphVo> graphVoList = graphInfoService.selectListByItemTypeAndId(ItemType.ROOM, id);

        //查找属性信息列表
        List<AttrValueVo> attrValueVoList = attrValueService.selectListByRoomId(id);

        //查找房间配套信息列表
        List<FacilityInfo> facilityInfoList = facilityInfoService.selectListByRoomId(id);

        //查找房间标签信息列表
        List<LabelInfo> labelInfoList = labelInfoService.getListByRoomId(id);

        //查找房间支付方式信息
        List<PaymentType> paymentTypeList = paymentTypeService.getListByRoomId(id);

        //查找房间费用信息
        List<FeeValueVo> feeValueVoList = feeValueService.getfeeValueVoListByApartmentId(roomInfo.getApartmentId());

        //查找房间租期信息
        List<LeaseTerm> leaseTermList = leaseTermService.getListByRoomId(id);

        //查询房间是否被删除
        Boolean isDelete = roomInfo.getIsDeleted()  == 1;

        //查询房间入住状态
        LambdaQueryWrapper<LeaseAgreement> leaseAgreementLambdaQueryWrapper = new LambdaQueryWrapper<LeaseAgreement>()
                .eq(LeaseAgreement::getRoomId, id)
                .in(LeaseAgreement::getStatus, LeaseStatus.SIGNED, LeaseStatus.WITHDRAWING, LeaseStatus.RENEWING);
        long count = leaseAgreementService.count(leaseAgreementLambdaQueryWrapper);
        Boolean isCheckIn = count > 0;

        BeanUtils.copyProperties(roomInfo, roomDetailVo);
        roomDetailVo.setApartmentItemVo(apartmentItemVo);
        roomDetailVo.setGraphVoList(graphVoList);
        roomDetailVo.setAttrValueVoList(attrValueVoList);
        roomDetailVo.setFacilityInfoList(facilityInfoList);
        roomDetailVo.setLabelInfoList(labelInfoList);
        roomDetailVo.setPaymentTypeList(paymentTypeList);
        roomDetailVo.setFeeValueVoList(feeValueVoList);
        roomDetailVo.setLeaseTermList(leaseTermList);
        roomDetailVo.setIsDelete(isDelete);
        roomDetailVo.setIsCheckIn(isCheckIn);
        browsingHistoryService.saveHistory(LoginUserContext.getLoginUser().getUserId(), id);
        return roomDetailVo;
    }

    @Override
    public IPage<RoomItemVo> getRoomItemVoPageByApartmentId(Page<RoomItemVo> page, Long id) {
        return roomInfoMapper.getRoomItemVoPageByApartmentId(page, id);
    }
}




